home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / ENTRPRIS / CALLBACK / CLBK_SVR.CLS < prev    next >
Encoding:
Visual Basic class definition  |  1996-11-23  |  6.8 KB  |  155 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CbServerClass"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Option Explicit
  11.  
  12. 'This sample demonstrates the use of an ole object ptr being passed to
  13. 'an external (and optionally remote) ole server.  The external ole server
  14. 'periodically calls a method on the passed object ptr. This has the effect
  15. 'of a server initiated callback to the client... which can be a much better
  16. 'app model that the polling a client app might have to do otherwise to
  17. 'find the status of a server.  Although this demo simply returns the time
  18. 'to the client, it could just as easily return data, news, or other information
  19. 'it has been told the client wants to know.  The benefit here is that the
  20. 'server does all the work looking for data the client might need... and the
  21. 'client does other work... only being interrupted when the server actually
  22. 'has something that interests it.
  23.  
  24. 'Note1: In this scenario, the client creates an object that is internal to it'
  25. 'It then creates an instance of the remote server and passes an object
  26. 'ptr to it of its internal object.  The server hangs onto the client's object,
  27. '"feeding it" from time to time.  At somepoint the client decides to
  28. 'disconnect (DropObjectReference) and the server does some clean up
  29. 'work in preparation for the client setting its reference to the server =
  30. 'Nothing.  Since this server has a visible form (servers usually will not
  31. 'have any visible displays except for debugging information), it unloads
  32. 'it at this point so that the instance will be closed by ole when the client
  33. 'sets the reference = Nothing.
  34.  
  35. 'Note2: Since the client created both the instance to its internal object
  36. 'and the instance to the server... it is important that the client be the one
  37. 'to close these instances by setting them = Nothing.  OLE errors will
  38. 'occur if the server tries to set the reference to the client = Nothing.
  39.  
  40. 'Note3: This app was developed very quickly... and should not be taken
  41. 'as gospel on how to build good servers.  For example, a good server
  42. 'should not have ANY msgbox calls outside of a debug mode.  (If a
  43. 'production server tried to display a msg for the user... it could wait a
  44. 'long time for someone to come by the server machine to click "OK".)
  45. 'This app also has very little error handling...it will be refined and cleaned
  46. 'up before final release.
  47.  
  48. 'Note4: The following class properties are critical for the behavior of
  49. 'the application:
  50. '
  51. '  "Instancing = Creatable SingleUse" This causes OLE to create a
  52. '           new physical instance of the class for each client.  This
  53. '           provides a simple programming model for creating parallel
  54. '           execution paths on the NT server, but requires a fair amount
  55. '           of memory to be allocated for each client.  Literally, SingleUse
  56. '           means the server is only used by a a single client at a time.
  57. '           A more efficient runtime model can be achieved if the setting
  58. '           of "Instancing = MultiUse" is set.  This causes OLE to only
  59. '           create an instance of the server for the first client... all other
  60. '           clients will get a ptr to the initial instance when they do their
  61. '           creates.  This model is more efficient from a memory standpoint,
  62. '           but OLE will only allow one client to use the class instance at
  63. '           a time... which could cause client blocking if the work the class
  64. '           does takes longer than the average client request interval.  A
  65. '           good compromise between these two implementation options is
  66. '           to use a pool of SingleUse servers.  In this case, 100 clients
  67. '           might have access to a pool of 10 class instances.  See the
  68. '           poolmngr sample for more details on this scenario.
  69. '
  70. '  "Public = True" This allows external apps to access this class through
  71. '           OLE.
  72. '  "Name = CbServerClass" This name is used as the class name in the second part
  73. '           of the progid.  The client references this progid to tell OLE what
  74. '           object it wants to start.
  75.  
  76. 'Note5:
  77. 'In the Tools.Options.Project dialog, it is very important that the project name
  78. 'be set.  This is used by the client to define the project name in the progid.  After
  79. 'creating an ole server exe, the exe must be run once so that it can register its
  80. 'OLE typeinfo data in the system registry.  After running it once, close the server
  81. 'manually and everything should be set for your client app to call the server through
  82. 'OLE.
  83.  
  84. 'Note6: Every time you build a new exe of your server, VB will generate a new
  85. 'unique ID for each of its classes.  Since this ID must be the same ID that is also
  86. 'registered in the client machine's registry, this constant changing of the ID can
  87. 'make keeping the client and the server working together a real chore.  To get
  88. 'around this, go to the Tools.Options.Project dialog and set the "Compatible OLE
  89. 'Server" field to a previous instance of the server exe.  Then, each time VB rebuilds
  90. 'your server exe, it will steal the classid(s) out of the previous version.  It will also
  91. 'do other checking to try to make sure you have maintained compatibility with earlier
  92. 'versions.
  93.  
  94. Private Sub Class_Terminate()
  95. '  Debug.Print "CbServerProj.CbServerClass has terminated"
  96. End Sub
  97. Private Sub Class_Initialize()
  98. '  Debug.Print "CbServerProj.CbServerClass has intialized"
  99. End Sub
  100.  
  101. Public Function AddObjectReference(Caller As Object) As Boolean
  102.   On Error GoTo AddObjectReferenceError
  103.   
  104.   Set gObjRef = Caller
  105.   frmMain.Timer1.Enabled = True
  106.   AddObjectReference = True
  107.   gbConnected = True
  108.   Exit Function
  109.  
  110. AddObjectReferenceError:
  111.   #If DEBUG_ON Then
  112.   MsgBox Error$, vbOKOnly + vbExclamation, "AddObjectReference - Error" & Str$(Err)
  113.   #End If
  114.   AddObjectReference = False
  115.   Exit Function
  116. End Function
  117.  
  118. Public Function DropObjectReference(Caller As Object) As Boolean
  119. Dim iCounter As Integer
  120. On Error GoTo DropObjReferenceError
  121.  
  122.   If gObjRef Is Caller Then
  123.     gbConnected = False
  124.     frmMain.Timer1.Enabled = False
  125.     Unload frmMain
  126.     DropObjectReference = True
  127.   Else
  128. '    Debug.Print "Caller not the same as ObjRef.  Unable to quit."
  129.     DropObjectReference = False
  130.   End If
  131. Exit Function
  132.  
  133. DropObjReferenceError:
  134.   #If DEBUG_ON Then
  135.   MsgBox Error$, vbOKOnly + vbExclamation, "DropObjectReference - Error" & Str$(Err)
  136.   #End If
  137.   DropObjectReference = False
  138.   Exit Function
  139.  
  140. End Function
  141.  
  142. Public Function SetInterval(iInterval As Integer) As Boolean
  143.     
  144.     On Error GoTo SetIntervalError
  145.     frmMain.Timer1.Interval = (iInterval * 1000)
  146.     frmMain.lblInterval.Caption = Format$(iInterval)
  147.     SetInterval = True
  148.     Exit Function
  149.     
  150. SetIntervalError:
  151.     SetInterval = False
  152.     Exit Function
  153.     
  154. End Function
  155.